package com.wokeMy.view.channe.tongfutong;

import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.MybankblindActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.wokeMy.view.DetailDhActivity;
import com.wokeMy.view.RecordCreditActivity;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;


public class TftZfActivity extends BaseActivity implements View.OnClickListener {

    TextView zfcard_tftzf,jscard_tftzf;
    TextView money_tftzf_et;
    RelativeLayout zhifu_tftzf_rl;
    RelativeLayout jieshuan_tftzf_rl;
    ImageView back;
    TextView jscard_tftzf_text_title;
    Button paybt;

    Data_bindcarlist listdata,payListdata;
    Intent intent;
    float money;
    int way_id,vip_level;
    String user_id,phone;
    String pay_card_no,card_no,expired_date,cvv,smsCode_bind;
    UnionPayChannel unionPayChannel;
    Dialog mydialog;

    Double yueMoney;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_tft_zf);
    }

    @Override
    public void initActivity() {
        zhifu_tftzf_rl.setOnClickListener(this);
        jieshuan_tftzf_rl.setOnClickListener(this);
        back.setOnClickListener(this);
        jscard_tftzf_text_title.setOnClickListener(this);
        paybt.setOnClickListener(this);
        money_tftzf_et.setText(money+" 元");

    }

    @Override
    public void initView() {
        money_tftzf_et = myFindViewById(R.id.money_tftzf_et);
        jscard_tftzf_text_title = myFindViewById(R.id.jscard_tftzf_text_title);

        zhifu_tftzf_rl = myFindViewById(R.id.zhifu_tftzf_rl);
        jieshuan_tftzf_rl = myFindViewById(R.id.jieshuan_tftzf_rl);
        zfcard_tftzf = myFindViewById(R.id.zfcard_tftzf);
        jscard_tftzf = myFindViewById(R.id.jscard_tftzf);
        back = myFindViewById(R.id.back_tftzf_iv);
        paybt = myFindViewById(R.id.pay_tftzf_bt);


    }

    @Override
    public void initData() {
        intent=getIntent();
        way_id = intent.getIntExtra ("way_id", 0);
        money = intent.getFloatExtra ("money", 0.01f);
        //        user_id = intent.getStringExtra("user_id");
        yueMoney=0.00;
        user_id=((MyApp)getApplication()).getDatas_load().getId();
        vip_level=(Integer.parseInt(((MyApp)getApplication()).getDatas_load().getUser_type()));
        //        unionPayChannel=(UnionPayChannel)intent.getSerializableExtra("unionPayChanne");
        //        Log.e("initData",money+" "+unionPayChannel.getId()+" "+unionPayChannel.get)
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dhjl_tv:
                Intent inte = new Intent (TftZfActivity.this, RecordCreditActivity.class);
                startActivity(inte);
                break;
            case R.id.detail_tv:
                Intent inte1 = new Intent (TftZfActivity.this, DetailDhActivity.class);
                startActivity(inte1);
                break;
            case R.id.back_tftzf_iv:
                finish();
                break;
            case R.id.jscard_tftzf_text_title:
                Intent inte2 = new Intent (TftZfActivity.this, RcTftActivity.class);
                inte2.putExtra ("way_id",way_id);
                startActivity(inte2);
                break;
            case R.id.pay_tftzf_bt:
                if (pay_card_no==null||pay_card_no.equals("")){
                    Toast.makeText(TftZfActivity.this,"付款未选择",Toast.LENGTH_SHORT).show();
                } else if(card_no==null||card_no.equals("")){
                    Toast.makeText(TftZfActivity.this,"收款卡未选择",Toast.LENGTH_SHORT).show();
                } else {
                    mydialog=  Util.createLoadingDialogTft(TftZfActivity.this);
                    tftZf();
                }
                //                Toast.makeText(TftZfActivity.this,"暂未开通",Toast.LENGTH_SHORT).show();
                break;
            case R.id.zhifu_tftzf_rl: // 付款卡
                Intent intent = new Intent (TftZfActivity.this, MybankblindActivity.class);
                intent.putExtra ("czhiortxian", "zhifucard");
                startActivityForResult (intent, Constant.REQUEST_ZHIFU);
                break;
            case R.id.jieshuan_tftzf_rl: // 收款卡
                Intent intent2 = new Intent (TftZfActivity.this, MybankblindActivity.class);
                intent2.putExtra ("czhiortxian", "zscard");
                startActivityForResult (intent2, Constant.REQUEST_JIESHUAN);
                break;
            default:
                break;
        }
    }

    //腾付通快捷支付接口
    private void  tftZf() {
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("amount",money);
        params.put("credit_card",pay_card_no);
        params.put("debit_card",card_no);
        ahc.setTimeout(120000);
        Log.e("腾付通快捷支付接口params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.TFTZF_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(mydialog);
                //判断状态码
                if(i == 200){
                    //获取结果
                    try {
                        String result = new String(bytes, "utf-8");
                        //                        Toast.makeText(TftZfActivity.this, result, Toast.LENGTH_SHORT).show();
                        Log.e("腾付通快捷支付接口Result", result);
                        try {

                            JSONObject jsonObject = new JSONObject(result);
                            if (jsonObject.get("flag").equals("success")) {
                                String info = jsonObject.getString("info");
                                if (jsonObject.has("data")){
                                    info=jsonObject.getJSONObject("data").getJSONObject("data").getString("message");
                                    String ordersts=jsonObject.getJSONObject("data").getJSONObject("data").getString("ordersts");
                                    switch (ordersts) {
                                        case "S":
                                            info="到账成功";
                                            break;
                                        case "F":
                                            info="处理中";
                                            break;
                                        case "W":
                                            info="交易失败";
                                            break;
                                        default:
                                            info="交易成功";
                                            break;
                                    }
                                }
                                tishi(info);
                            } else {
                                String info = jsonObject.getString("info");
                                if (jsonObject.has("data")){
                                    info=jsonObject.getJSONObject("data").getJSONObject("data").getString("message");
                                }
                                tishi(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(mydialog);
            }
        });
    }

    //腾付通修改费率接口
    private void  fixFl() {
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("type","00");// 类型  00=>D+0结算 01=》T+1结算
        params.put("way",way_id);
        Log.e("腾付通修改费率接口params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.FIXFL_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(mydialog);
                //判断状态码
                if(i == 200){
                    //获取结果
                    try {
                        String result = new String(bytes, "utf-8");
                        //                        Toast.makeText(TftZfActivity.this, result, Toast.LENGTH_SHORT).show();
                        Log.e("腾付通修改费率接口Result", result);
                        try {

                            JSONObject jsonObject = new JSONObject(result);
                            if (jsonObject.get("flag").equals("success")) {
                            } else {
                                Toast.makeText(TftZfActivity.this, jsonObject.getString("info"), Toast.LENGTH_LONG).show();

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(mydialog);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //        Log.e("RESPONSE_JIESHUAN",resultCode+"");
        if (resultCode==Constant.RESPONSE_ZHIFU){
            payListdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            pay_card_no=payListdata.cardno;
            zfcard_tftzf.setText(pay_card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+pay_card_no.substring(pay_card_no.length()-3,pay_card_no.length()));
        }else if(resultCode==Constant.RESPONSE_JIESHUAN){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            card_no=listdata.cardno;
            jscard_tftzf.setText(card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+card_no.substring(card_no.length()-3,card_no.length()));
//            tftRz();
        }
    }

}
